package com.team.game.Game.MapHelper;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.view.MotionEvent;

import com.team.game.Assets;
import com.team.game.framework.Game;
import com.team.game.framework.impl.MainActivity;

import java.io.IOException;
import java.io.InputStream;

import static com.team.game.framework.impl.Scale_X_Y.Size_X;
import static com.team.game.framework.impl.Scale_X_Y.Size_Y;

public class Border {

    public final static RectF borderRect = new RectF (0, Size_Y - Size_X / 4, Size_X, Size_Y);

    private Bitmap border_top, border_bottom;
    private Matrix matrix_top, matrix_bottom;

    private RectF button1;
    private RectF button2;
    private RectF button3;
    private RectF button4;

    private Paint paint;

    public Border(Game game, int location){
        init ();

        if(location != 0) {
            paint.setAlpha(0);
        }
        else
            i=0;
        InputStream inputStream;
        try {
            inputStream = MainActivity.manager.open ("Picture/Border/border_top" + location + ".png");
            border_top = Bitmap.createScaledBitmap (BitmapFactory.decodeStream (inputStream), Size_X, Size_Y, false);

            inputStream = MainActivity.manager.open ("Picture/Border/border_bottom" + location + ".png");
            border_bottom = Bitmap.createScaledBitmap (BitmapFactory.decodeStream (inputStream), (Size_X), (Size_X/4), false);

        } catch (IOException e) {
            e.printStackTrace ();
        }
    }

    public void present (Canvas canvas) {
        canvas.drawBitmap (border_top, matrix_top, paint);
        canvas.drawBitmap (border_bottom, matrix_bottom, paint);
    }

    private int i = 18;
    private int alpha = 255;
    private boolean startAnim = false, stopAnim = false;

    public void update () {
        //TODO Заменить на другой еффект
        if(stopAnim){
            if (i < 17) {
                i++;
                alpha -= 15;
                paint.setAlpha (alpha);
            } else{
                stopAnim = false;
                paint.setAlpha (0);
            }
        }
        if(startAnim){
            if (i < 17) {
                i++;
                alpha += 15;
                paint.setAlpha (alpha);
            } else{
                startAnim = false;
                paint.setAlpha (255);
            }
        }
    }

    public void start (){
        alpha = 0;
        startAnim = true;
        i = 0;
    }

    public void disappear (){
        alpha = 255;
        stopAnim = true;
        i = 0;
    }

    public int touch (MotionEvent event) {

            if (Assets.inRect (button1, event))
            {
                return 1;
            }
            else if (Assets.inRect (button2, event))
            {
                return 2;
            }
            else if (Assets.inRect (button3, event))
            {
                return 3;
            }
            else if (Assets.inRect (button4, event))
            {
                return 4;
            }

        return 0;
    }

    private void init(){
         //устанавливаем зоны кнопок
        button1 = new RectF (0                ,Size_Y - Size_X/4, Size_X/4         , Size_Y);
        button2 = new RectF (Size_X/4         ,Size_Y - Size_X/4, Size_X/2         , Size_Y);
        button3 = new RectF (Size_X/2         ,Size_Y - Size_X/4, Size_X - Size_X/4, Size_Y);
        button4 = new RectF (Size_X - Size_X/4,Size_Y - Size_X/4,       Size_X           , Size_Y);

        matrix_top = new Matrix ();
        matrix_top.preTranslate (0, 0);

        matrix_bottom = new Matrix ();
        matrix_bottom.preTranslate (0, Size_Y - Size_X/4);

        paint = new Paint ();
    }

}
